const fs = require('fs');
const mongoose = require('mongoose');
const xlsx = require('node-xlsx').default;

const gzf = require('./gzfSchema').gzf;

let list = ['QUA_DATE', 'SHOUCCBSJ_GZ', 'LHMC_ID', 'WAIT_TPYE', 'OUTLH_FLAG', 'SHOUCCBSJ_AJ', 'SHOULHZH', 'REMARK', 'RUHSJ', 'RZQK', 'RGQK', 'PAIX', 'OUT_TIME', 'LHCYXXB_ID', 'SFZH', 'XINGM', 'NUM', 'SHOUCCBSJ', 'SQB_ID'];

let data = [];
data[0] = list;

gzf.find({}, { _id: 0 }).lean().exec(function (err, rel) {
    if (err) {
        console.log(err);
        return;
    }
    let total = rel.length;
    for (let i = 0; i < total; i++) {
        let arr = [];
        console.log(i);
        let obj = rel[i];
        for (let k in list) {
            arr[k] = obj[list[k]];
        }
        data[data.length] = arr;
    }

    let buffer = xlsx.build([{ name: "sheet1", data: data }]); // Returns a buffer

    fs.writeFileSync('gzf.xlsx', buffer, { flag: 'w' });
    console.log('导出完成...');

    mongoose.disconnect();
});

